package com.zc.springmvc.tool;

import java.lang.reflect.InvocationTargetException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.codec.binary.Base64;



public class BeanUtil {
	
	/**
	 * 
	 * @param obj 源数�?
	 * @param objClass 
	 * @return
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * @throws InstantiationException
	 * @throws ClassNotFoundException
	 */
	public static<T> T copyProperties(Object obj,Class<?> objClass) {
		    T t=null;
			try {
				    Class<?> ca = Class.forName(objClass.getName());
				    t=(T) ca.newInstance();
					BeanUtils.copyProperties(t, obj);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return t;
		
	}
	
	
	/**
	 * base64编码加密
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static String encryptionBASE64(String key)  { 
		 Base64 base64 = new Base64();
		 byte[] enbytes = base64.encode(key.getBytes());	 
		 return new String(enbytes);
	    
	}  
	  
	/**
	 * base64编码解密
	 * @param val
	 * @return
	 * @throws Exception
	 */
	public static String decryptionBASE64(String val)  {  
		Base64 base64 = new Base64();
		byte[] enbytes=val.getBytes();
		byte[] debytes =base64.decode(enbytes);
		return new String(debytes);
	}  
	
	/**
	 * MD5加密
	 * @param arg
	 * @return
	 * @throws Exception
	 */
	public static String encryptMD5(String arg)  {  
		  
	    MessageDigest md5= null;
		try {
			md5= MessageDigest.getInstance("MD5");
			byte[] data=arg.getBytes();
		    md5.update(data);  
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
	    
	    
	    return encryptionBASE64(md5.digest().toString());	  
	}  
	
	
	
	public static void main(String[]args) throws Exception{
		String s=encryptionBASE64("中文");
		System.out.println(s);
		String a=decryptionBASE64("W0JAMjQ5YjczMzY=");
		System.out.println(a);
		
		System.out.println(encryptMD5("SLX123456"));
		
	}
	

	
	
	
	
	

}
